package cn.zuzi.mzitu.utils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import org.apache.commons.io.FileUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.Connection.Response;
import org.jsoup.select.Elements;

public class PicUtils {

	public static final String encoding = "UTF-8";
	public static final String SAVE_FILE = "d:/piccc";

	/**
	 * 获取 图片 页码数
	 * @param content
	 * @return
	 */
	public static int getPicNum(String content) {

		Elements byTag = Jsoup.parse(content).getElementsByTag("a");
		String text = byTag.get(byTag.size() - 2).text();
		return Integer.parseInt(text);
	}

	public static void downPic(String urlStr, String fileName) {
		try {
			URL url = new URL(urlStr);
			String referer = url.getProtocol() + "://" + url.getHost();

			Connection ref = Jsoup.connect(url.toString()).ignoreContentType(true).referrer(referer).timeout(50000);
			Response execute;

			execute = ref.execute();

			BufferedInputStream bodyStream = execute.bodyStream();
			FileUtils.copyInputStreamToFile(bodyStream, new File(SAVE_FILE,fileName));

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取文件后缀名
	 * @param fileStr
	 * @return
	 */
	public static String getFieExtName(String fileStr){
		return fileStr.substring(fileStr.lastIndexOf("."));
	}
	
}
